package com.glory.brepda.activity.mfg;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class PackageMoveActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvCircuitName)
    TextView tvCircuitName;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvPackageFrom)
    TextView tvPackageFrom;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.tvStates)
    TextView tvStates;
    @BindView(R.id.etStorageID)
    FreeUI_ClearEditText etStorageID;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_package_move;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);

        initEdit(etLot, s -> searchLotInfo(s));
    }

    public void searchLotInfo(String lotID) {
        FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                }.getType(),getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    LotInfoEntity body = response.body().getResponse().getBody();
                    tvCircuitName.setText(body.getPRODUCTMODEL());
                    tvCustomCode.setText(body.getLOT().getCUSTOMERCODE());
                    tvPackageFrom.setText(body.getLOT().getMAINMATTYPE());
                    tvStates.setText(body.getLOT().getSTATE());
                    tvLotNum.setText(body.getLOT().getMAINQTY());
                    etLot.setText(body.getLOT().getLOTID());
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTQUERY");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","MOVESTORAGELOTQUERY");
        map.addProperty("LOTID",lotID);
        NetUtils.requestPostNet("CPC.LOTQUERY","LOTQUERY",PackageMoveActivity.this,map,callBack);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        if (getText(etLot).equals("") || getText(etStorageID).equals("")) {
            showToast("批次号和货位号不能为空", false);
            return;
        }
        showSubmitDialog(() -> {
            FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                    (new TypeToken<BaseResponseBean<JSONObject>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                        showConfirmDialog(PackageMoveActivity.this,"包装移库成功!");
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<JSONObject>> response) {
                    super.onError(response);
                    loadError(response.getException(),"PACKAGEMOVE");

                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("LOTID",getText(etLot));
            map.addProperty("STORAGEID",getText(etStorageID));
            NetUtils.requestPostNet("CPC.PACKAGEMOVE","PACKAGEMOVE",PackageMoveActivity.this,map,callBack);
        });

    }
}